<template>
  <div>
    <div v-show="(!isEdit&&!isDetail)" class="page-index">
      <div>
        <div class="table-search">
          <el-form inline>
            <el-form-item label="姓名">
              <el-input v-model="queryParams.name" size="mini" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="发展状态">
              <el-select clearable v-model="queryParams.develStage" size="mini" placeholder="请选择" >
                <el-option
                  v-for="(item,index) in statusList"
                  :key="index"
                  :label="item"
                  :value="index"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="时间节点">
              <el-date-picker
                v-model="dateRange"
                size="mini"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                @change="changeDate"
              />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" @click="search">搜索</el-button>
              <el-button size="mini" @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
          <el-form inline>
            <el-form-item>
              <el-button size="mini" type="primary" @click="openAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="table-box">
          <el-table
            :data="tableData"
            height="100%"
          >
            <el-table-column type="index" label="序号" align="center" width="50" />
            <el-table-column prop="name" label="姓名" align="center" width="120" />
            <el-table-column prop="developmentOrgName" label="发展组织" align="center" min-width="180" />
            <el-table-column prop="affiliationName" label="所属支部" align="center" min-width="180" />
            <el-table-column prop="develStage" label="发展状态" align="center" width="180">
              <template slot-scope="scope">
                {{ statusList[scope.row.develStage] }}
              </template>
            </el-table-column>
            <el-table-column prop="operatingTime" label="更新时间" align="center" min-width="160" />
            <el-table-column prop="developmentTime" label="时间节点" align="center" width="120" />
            <el-table-column label="操作" align="center" width="120">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="openDetail(scope.row)"
                >查看</el-button>
                <el-button
                  type="text"
                  size="mini"
                  @click="openEdit(scope.row)"
                >编辑</el-button>
                <el-button
                  type="text"
                  size="mini"
                  style="color:#F56C6C;"
                  @click="openDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getData"
        />
      </div>
    </div>
    <addOrEdit v-show="isEdit" ref="addOrEdit" @getData="getData" @goback="editGoback" />
    <detailPage v-show="isDetail" ref="detailPage" @detailToEdit="detailToEdit" @goback="detailGoback" />
  </div>
</template>
<script>
import { queryByCondition, queryById, deleteById } from '@/api/partyMemberManagement/developmentOfMembers'
import addOrEdit from './components/addOrEdit'
import detailPage from './components/detail'
export default {
  components: {
    addOrEdit,
    detailPage
  },
  data () {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: '',
        develStage: '',
        developmentStartTime: '',
        developmentEndTime: ''
      },
      dateRange: [],
      total: 0,
      tableData: [],
      isDetail: false,
      isEdit: false,
      statusList: ['积极分子', '发展对象', '预备党员', '已转正', '延长预备期', '取消预备党员资格']
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    getData () {
      queryByCondition(this.queryParams).then((res) => {
        this.tableData = res?.data?.records || []
        this.total = res?.data?.total || 0
      })
    },
    search () {
      this.queryParams.pageNo = 1
      this.getData()
    },
    openAdd () {
      this.isEdit = true
      this.$refs.addOrEdit.init({})
    },
    openEdit (row) {
      queryById({ id: row.id }).then((res) => {
        this.isEdit = true
        this.$refs.addOrEdit.init(res?.data || row)
      })
    },
    openDelete (row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById({ id: row.id }).then((res) => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
    },
    openDetail (row) {
      queryById({ id: row.id }).then((res) => {
        this.isDetail = true
        this.$refs.detailPage.init(res?.data || row)
      })
    },
    editGoback (isQuery) {
      this.isEdit = false
      if (isQuery) {
        this.getData()
      }
    },
    detailGoback () {
      this.isDetail = false
    },
    detailToEdit (row) {
      this.isDetail = false
      this.openEdit(row)
    },
    changeDate (val) {
      this.queryParams.developmentStartTime = val[0]
      this.queryParams.developmentEndTime = val[1]
    },
    reset () {
      this.queryParams.name = ''
      this.queryParams.developmentStartTime = ''
      this.queryParams.developmentEndTime = ''
      this.queryParams.develStage = ''
      this.dateRange = []
      this.search()
    }
  }
}
</script>
<style scoped lang="scss">
.page-index {
  padding-top: 24px;
  .table-search {
    display: flex;
    justify-content: space-between;
    padding-left:16px;
  }
  .table-box {
    height: calc(100vh - 230px);
  }
}

</style>
